import 'package:flutter/cupertino.dart';

/// @author: pengboboer
/// @createDate: 2022/1/6
class ClickWidget extends StatelessWidget {
  final Widget child;
  final VoidCallback onTap;
  final bool isAvoidMultiClick;

  const ClickWidget(
      {Key? key,
      required this.child,
      required this.onTap,
      this.isAvoidMultiClick = true})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: isAvoidMultiClick ? _AvoidMultiClick().onClick(onTap) : onTap,
        behavior: HitTestBehavior.opaque,
        child: child);
  }
}

class _AvoidMultiClick {
  static const Duration _KDelay = Duration(milliseconds: 500);

  bool isClicking = false;

  VoidCallback onClick(VoidCallback voidCallback, {Duration delay = _KDelay}) {
    return () {
      if (!isClicking) {
        voidCallback();
        isClicking = true;
        Future.delayed(_KDelay, () {
          isClicking = false;
        });
      }
    };
  }
}
